package com.ydgk.servlet;

import com.ydgk.bean.Admin;
import com.ydgk.dao.AdminDao;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet(name = "LoginServlet", urlPatterns = "/LoginServlet.action")
public class LoginServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String aname = request.getParameter("aname").trim();
        String apass = request.getParameter("apass").trim();
        AdminDao adminDao = new AdminDao();
        Admin admin = adminDao.findByName(aname);
        if (admin != null) {
            if (apass.equals(admin.getApass())) {
                request.setAttribute("aname",aname);
                request.setAttribute("message","success");
                RequestDispatcher rd = request.getRequestDispatcher("login.jsp");
                rd.forward(request, response);
            } else {
                request.setAttribute("message","ApassFailed");
                RequestDispatcher rd = request.getRequestDispatcher("login.jsp");
                rd.forward(request, response);
            }
        }else {
            request.setAttribute("message","AnameFailed");
            RequestDispatcher rd = request.getRequestDispatcher("login.jsp");
            rd.forward(request, response);
        }
    }

}
